package com.dxb.shardingjdbcmybatistenent;

import io.shardingsphere.api.HintManager;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * ClassName：com.dxb.shardingjdbcmybatistenent.HintAop <br>
 * Description：TODO <br>
 *
 * @author 戴晓波 <br>
 * Date 2022/1/26/026 14:36 <br>
 * @version v1.0 <br>
 */
@Aspect
@Order(1)
@Component
public class HintAop {
    public static int count = 0;

    @Pointcut("execution(* com.dxb.shardingjdbcmybatistenent.repository..*.*(..))")
    public void switchDataSource() {
    }

    @Around("switchDataSource()")
    public Object aroundSwitchData(ProceedingJoinPoint joinPoint) throws Throwable {

        try (HintManager hintManager = HintManager.getInstance()) {
            hintManager.setDatabaseShardingValue("database" + count++ % 2);
            return joinPoint.proceed();
        }
    }
}
